"""
Case Type   : 功能测试
Case Name   : 流控参数验证
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.主机设置流控参数为-1
gs_guc set -N hostname -c 'recovery_time_target=-1';
    2.主机设置流控参数为3601
gs_guc set -N hostname -c 'recovery_time_target=3601';
    3.主机设置流控参数为0
gs_guc set -N hostname -c 'recovery_time_target=0';
    4.主机设置流控参数为0
gs_guc set -N hostname -c 'recovery_time_target=3600';
Expect      :
    1.设置失败，合理报错
    2.设置失败，合理报错
    3.设置成功
    4.设置成功
History     :
"""

import unittest
import os
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from yat.test import Node
logger = Logger()

class TestRealtimeBuild_Control0001(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.com = Common()
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.param = 'recovery_time_target'
        self.src = self.com.show_param(self.param)

        logger.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_shared_storage(self):
        logger.info('获取主节点hostname')
        check_cmd = f'hostname'
        logger.info(check_cmd)
        hostname = self.pri_node.sh(check_cmd).result()
        logger.info(hostname)

        text = '-----step1:主机设置流控参数为3601 expect:设置失败，合理报错-----'
        logger.info(text)
        set_param = self.pri_sh.execute_gsguc('set',
                                             '"recovery_time_target" is incorrect',
                                             f"{self.param} = 3601", 
                                             hostname)
        logger.info(set_param)
        self.assertTrue(set_param, '参数修改异常')

        text = '-----step2:主机设置流控参数为3601 expect:设置失败，合理报错-----'
        logger.info(text)
        set_param = self.pri_sh.execute_gsguc('set',
                                             '"recovery_time_target" is incorrect',
                                             f"{self.param} = 3601", 
                                             hostname)
        logger.info(set_param)
        self.assertTrue(set_param, '参数修改异常')

        text = '-----step3:主机设置流控参数为0 expect:设置成功-----'
        logger.info(text)
        set_param = self.pri_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.param} = 0", 
                                             hostname)
        logger.info(set_param)
        self.assertTrue(set_param, '参数修改异常')

        text = '-----step4:主机设置流控参数为3600 expect:设置成功-----'
        logger.info(text)
        set_param = self.pri_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.param} = 3600", 
                                             hostname)
        logger.info(set_param)
        self.assertTrue(set_param, '参数修改异常')

    def tearDown(self):
        text = '-----step5:恢复环境为初始值，恢复集群状态; expect:恢复初始值成功，集群状态正常-----'
        logger.info('-----恢复参数初始值-----')
        recov_param = self.pri_sh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f"{self.param} = '{self.src}'")
        logger.info(recov_param)

        logger.info('-----重启集群-----')
        restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start')
        logger.info(restart)

        self.assertTrue(recov_param, '参数修改失败')
        self.assertIn(self.constant.cm_start_success_msg, restart,
                      '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
